package com.qq.first_project.controller.admin;

import com.qq.first_project.common.R;
import com.qq.first_project.entity.User;
import com.qq.first_project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin //跨域
@RequestMapping("/admin/user")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @return
     */
    @PostMapping("/login")
    R login(@RequestParam("username") String username, @RequestParam("password") String password){
        User user = userService.checkUser(username,password);
        //判断是否执行成功
        if(user != null){
            return R.ok().data("data", user);
        }else{
            return R.error().message("用户名或密码错误");
        }
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("info")
    public R info() {
         return R.ok().data("data",userService.list(null));
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("logout")
    public R logout(){
        return R.ok().data("data","logout");
    }
}
